<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'销售员转化率'),
);
if($_SESSION['juese_id']>6 && $_SESSION['juese_id']<15){
	exi('您无权访问！','error');
}
//客户列表
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('status'=>1,'uniacid'=>$_SESSION['uniacid']);
	$workerids=$this->model('worker')->getmyworkers(6);
	$workerids=$workerids?array_keys($workerids):-1;
	$params['id']=$workerids;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:200;
	$memberparams=array('status >='=>0,'uniacid'=>$_SESSION['uniacid']);
	$shmennumparams=$shmennum2params=$tingnumparams=$ting2numparams=$xuenumparams=$tuifeenumparams=$memberparams;
	$shmennumparams=array('uniacid'=>$_SESSION['uniacid']);
	$shmennum2params['status >=']=0;
	$tuifeenumparams['status']=array(101,102,103,105,106);
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		//$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$between=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$myhebaonumparams=$yesmyhebaonumparams=$hebaonumparams=$baonumparams=array('type'=>1,'status'=>1,'uniacid'=>$_SESSION['uniacid']);
	$baonumparams2=$yesfeeparams=array('type'=>array(1,2,4,5),'status'=>array(1,3,4,5),'uniacid'=>$_SESSION['uniacid']);
	$tuifeenumparams=array('type'=>array(0,1),'status'=>4,'uniacid'=>$_SESSION['uniacid']);
	$yesfenfeeparams=$fenfeeparams=array('type'=>2,'uniacid'=>$_SESSION['uniacid']);
	$memberparams['type']=32;
	$shmennumparams['type']=$shmennum2params['type']=32;
	$tingnumparams['type']=40;
	$ting2numparams['type']=42;
	$xuenumparams['type']=41;
	if(!$between){
		$memberparams['createtime >']=0;
		$shmennumparams['createtime >']=0;
		$shmennum2params['createtime >']=0;
		$tingnumparams['createtime >']=0;
		$ting2numparams['createtime >']=0;
		$xuenumparams['createtime >']=0;
		$baonumparams['jiao_time >']=0;
		$baonumparams2['jiao_time >']=0;
		$tuifeenumparams['tuidaytime >']=0;
		$hebaonumparams['jiao_time >']=0;
		$myhebaonumparams['jiao_time >']=0;
		$fenfeeparams['jiao_time >']=0;
	}else{
		$memberparams['createtime between']=$between;
		$shmennumparams['createtime between']=$between;
		$shmennum2params['createtime between']=$between;
		$tingnumparams['createtime between']=$between;
		$ting2numparams['createtime between']=$between;
		$xuenumparams['createtime between']=$between;
		$baonumparams['jiao_time between']=$between;
		$baonumparams2['jiao_time between']=$between;
		$tuifeenumparams['tuidaytime between']=$between;
		$hebaonumparams['jiao_time between']=$between;
		$myhebaonumparams['jiao_time between']=$between;
		$fenfeeparams['jiao_time between']=$between;
	}
	$yestuifeenumparams=$tuifeenumparams;
	$list=pdo_getslice('jy_yxw_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
		$yesyue = mktime(0,0,0,date('m')-1,1,date('Y'));
		//$baonumparams2['jiao_time >']=$nowyue;
		$yesfeeparams['jiao_time between']=array($yesyue,$nowyue);
		$yesmyhebaonumparams['jiao_time between']=array($yesyue,$nowyue);
		$yesfenfeeparams['jiao_time between']=array($yesyue,$nowyue);
		$yestuifeenumparams['tuidaytime between']=array($yesyue,$nowyue);
		$hebaonumparams['workerheid >']=0;
		foreach($list as &$item){
			$yestuifeenumparams['worker6id']=$myhebaonumparams['workerheid']=$hebaonumparams['worker6id']=$baonumparams2['worker6id']=$shmennumparams['worker6id']=$shmennum2params['worker6id']=$tingnumparams['worker6id']=$ting2numparams['worker6id']=$xuenumparams['worker6id']=$baonumparams['worker6id']=$tuifeenumparams['worker6id']=$memberparams['worker6id']=$item['id'];
			$item['memberall']=pdo_count('jy_yxw_workerlog',$memberparams);
			$item['shangmenall']=pdo_count('jy_yxw_workerlog',$shmennumparams);
			$item['shangmenall2']=pdo_count('jy_yxw_workerlog',$shmennum2params);
			$item['tingnum']=pdo_count('jy_yxw_workerlog',$tingnumparams);
			$item['tingnum2']=pdo_count('jy_yxw_workerlog',$ting2numparams);
			$item['xuenum']=pdo_count('jy_yxw_workerlog',$xuenumparams);
			$item['tuinum']=pdo_count('jy_yxw_tuifee_log',$tuifeenumparams);
			if($item['memberall']>0){
				$item['shangmen2rate']=price($item['shangmenall2']*100/$item['memberall']);
			}
			if($item['shangmenall']>0){
				$item['ting1rate']=price($item['tingnum']*100/$item['shangmenall']);
			}
			if($item['tingnum']>0){
				$item['ting2rate']=price($item['tingnum2']*100/$item['tingnum']);
			}
			if($item['tingnum2']>0){
				$item['xue1rate']=price($item['xuenum']*100/$item['tingnum2']);
			}
			$item['baonum']=pdo_count('jy_yxw_infee_log',$baonumparams);
			if($item['tuinum']>0){
				//$item['baonum'] -=$item['tuinum'];
			}
			$item['hebaonum']=pdo_count('jy_yxw_infee_log',$hebaonumparams)/2;
			$item['myhebaonum']=pdo_count('jy_yxw_infee_log',$myhebaonumparams)/2;
			$item['baonum']=$item['baonum']-$item['hebaonum']+$item['myhebaonum'];
			if($item['shangmenall']>0){
				$item['bao2rate']=price($item['baonum']*100/$item['shangmenall']);
			}
			$item['danfee1']=pdo_getsum('jy_yxw_infee_log',$baonumparams2,'fee');
			if($item['xuenum']>0){
				$item['bao1rate']=price($item['baonum']*100/$item['xuenum']);
			}
			$item['tuifee']=pdo_getsum('jy_yxw_tuifee_log',$tuifeenumparams,'tuifee');
			$item['tuiisfee']=pdo_getsum('jy_yxw_tuifee_log',$tuifeenumparams,'isfee');
			$item['tuiisfee']=$item['tuiisfee']?:0;
			$item['yestuiisfee']=pdo_getsum('jy_yxw_tuifee_log',$yestuifeenumparams,'isfee');
			//销售业绩
			$item['now']=$this->model('count')->getyeji($item,$between);
			//上月业绩
			$item['yes']=$this->model('count')->getyeji($item,array($yesyue,$nowyue));
			$item['paixu']=$item['now']+$item['shangmen2rate'];
		}
	}
	$heji=array('title'=>'合计');
	paixu($list,'paixu','desc');
	$isnum=$psize*($pindex-1);
	foreach($list as $k=>&$v){
		$v['rank']=$k+1+$isnum;
		$heji['memberall'] +=$v['memberall'];
		$heji['shangmenall'] +=$v['shangmenall'];
		$heji['shangmenall2'] +=$v['shangmenall2'];
		$heji['tingnum'] +=$v['tingnum'];
		$heji['tingnum2'] +=$v['tingnum2'];
		$heji['xuenum'] +=$v['xuenum'];
		$heji['baonum'] +=$v['baonum'];
		$heji['tuinum'] +=$v['tuinum'];
		$heji['now'] +=$v['now'];
		$heji['yes'] +=$v['yes'];
	}
	if($heji['memberall']>0){
		$heji['shangmen2rate']=price($heji['shangmenall2']*100/$heji['memberall']);
	}
	if($heji['shangmenall']>0){
		$heji['ting1rate']=price($heji['tingnum']*100/$heji['shangmenall']);
	}
	if($heji['tingnum']>0){
		$heji['ting2rate']=price($heji['tingnum2']*100/$heji['tingnum']);
	}
	if($heji['tingnum2']>0){
		$heji['xue1rate']=price($heji['xuenum']*100/$heji['tingnum2']);
	}
	if($heji['xuenum']>0){
		$heji['bao1rate']=price($heji['baonum']*100/$heji['xuenum']);
	}
	if($heji['shangmenall']>0){
		$heji['bao2rate']=price($heji['baonum']*100/$heji['shangmenall']);
	}
	$list[]=$heji;
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'姓名','type'=>'string'),
			array('action'=>'/counts.juese6from/','title'=>'渠道率','type'=>'href','show'=>array('id >'=>0)),
			array('name'=>'shangmenall','title'=>'总接待','type'=>'string'),
			array('name'=>'shangmenall2','title'=>'有效上接待','type'=>'string'),
			array('name'=>'tingnum','title'=>'体验','type'=>'string'),
			array('name'=>'tingnum2','title'=>'体用','type'=>'string'),
			array('name'=>'xuenum','title'=>'试用','type'=>'string'),
			array('name'=>'baonum','title'=>'成交','type'=>'string'),
			array('name'=>'tuinum','title'=>'退成交','type'=>'string'),
			//array('name'=>'shangmen2rate','title'=>'有效接待率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'ting1rate','title'=>'接待-体验率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'ting2rate','title'=>'体验体用率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'xue1rate','title'=>'体用试用率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'bao1rate','title'=>'试用成交率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'bao2rate','title'=>'总成交率','type'=>'unitstring','unit'=>'%'),
			array('name'=>'now','title'=>'销售业绩','type'=>'unitstring','unit'=>'元'),
			array('name'=>'rank','title'=>'排名','type'=>'string'),
			array('name'=>'yes','title'=>'上月业绩','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'销售员名称','name'=>'title','help'=>''),
			array('name'=>'createtime','width'=>15,'title'=>'时间段','type'=>'time_range')
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	//客户列表
	$return['params']=array();
	$params=array('status'=>1,'uniacid'=>$_SESSION['uniacid']);
	$workerids=$this->model('worker')->getmyworkers(6);
	$workerids=$workerids?array_keys($workerids):-1;
	$params['id']=$workerids;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$memberparams=array('status >='=>0,'uniacid'=>$_SESSION['uniacid']);
	$shmennumparams=$shmennum2params=$tingnumparams=$ting2numparams=$xuenumparams=$tuifeenumparams=$memberparams;
	$shmennumparams=array('uniacid'=>$_SESSION['uniacid']);
	$shmennum2params['status >=']=0;
	$tuifeenumparams['status']=array(101,102,103,105,106);
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		//$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$between=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$myhebaonumparams=$yesmyhebaonumparams=$hebaonumparams=$baonumparams=array('type'=>1,'status'=>1,'uniacid'=>$_SESSION['uniacid']);
	$baonumparams2=$yesfeeparams=array('type'=>array(1,2,4,5),'status'=>array(1,3,4,5),'uniacid'=>$_SESSION['uniacid']);
	$tuifeenumparams=array('type'=>array(0,1),'status'=>4,'uniacid'=>$_SESSION['uniacid']);
	$yesfenfeeparams=$fenfeeparams=array('type'=>2,'uniacid'=>$_SESSION['uniacid']);
	$memberparams['type']=32;
	$shmennumparams['type']=$shmennum2params['type']=32;
	$tingnumparams['type']=40;
	$ting2numparams['type']=42;
	$xuenumparams['type']=41;
	if(!$between){
		$memberparams['createtime >']=0;
		$shmennumparams['createtime >']=0;
		$shmennum2params['createtime >']=0;
		$tingnumparams['createtime >']=0;
		$ting2numparams['createtime >']=0;
		$xuenumparams['createtime >']=0;
		$baonumparams['jiao_time >']=0;
		$baonumparams2['jiao_time >']=0;
		$tuifeenumparams['tuidaytime >']=0;
		$hebaonumparams['jiao_time >']=0;
		$myhebaonumparams['jiao_time >']=0;
		$fenfeeparams['jiao_time >']=0;
	}else{
		$memberparams['createtime between']=$between;
		$shmennumparams['createtime between']=$between;
		$shmennum2params['createtime between']=$between;
		$tingnumparams['createtime between']=$between;
		$ting2numparams['createtime between']=$between;
		$xuenumparams['createtime between']=$between;
		$baonumparams['jiao_time between']=$between;
		$baonumparams2['jiao_time between']=$between;
		$tuifeenumparams['tuidaytime between']=$between;
		$hebaonumparams['jiao_time between']=$between;
		$myhebaonumparams['jiao_time between']=$between;
		$fenfeeparams['jiao_time between']=$between;
	}
	$yestuifeenumparams=$tuifeenumparams;
	$list=pdo_getall('jy_yxw_worker',$params,$fields,'','id desc');
	if($list){
		$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
		$yesyue = mktime(0,0,0,date('m')-1,1,date('Y'));
		//$baonumparams2['jiao_time >']=$nowyue;
		$yesfeeparams['jiao_time between']=array($yesyue,$nowyue);
		$yesmyhebaonumparams['jiao_time between']=array($yesyue,$nowyue);
		$yesfenfeeparams['jiao_time between']=array($yesyue,$nowyue);
		$yestuifeenumparams['tuidaytime between']=array($yesyue,$nowyue);
		$hebaonumparams['workerheid >']=0;
		foreach($list as &$item){
			$yestuifeenumparams['worker6id']=$myhebaonumparams['workerheid']=$hebaonumparams['worker6id']=$baonumparams2['worker6id']=$shmennumparams['worker6id']=$shmennum2params['worker6id']=$tingnumparams['worker6id']=$ting2numparams['worker6id']=$xuenumparams['worker6id']=$baonumparams['worker6id']=$tuifeenumparams['worker6id']=$memberparams['worker6id']=$item['id'];
			$item['memberall']=pdo_count('jy_yxw_workerlog',$memberparams);
			$item['shangmenall']=pdo_count('jy_yxw_workerlog',$shmennumparams);
			$item['shangmenall2']=pdo_count('jy_yxw_workerlog',$shmennum2params);
			$item['tingnum']=pdo_count('jy_yxw_workerlog',$tingnumparams);
			$item['tingnum2']=pdo_count('jy_yxw_workerlog',$ting2numparams);
			$item['xuenum']=pdo_count('jy_yxw_workerlog',$xuenumparams);
			$item['tuinum']=pdo_count('jy_yxw_tuifee_log',$tuifeenumparams);
			if($item['memberall']>0){
				$item['shangmen2rate']=price($item['shangmenall2']*100/$item['memberall']);
			}
			if($item['shangmenall']>0){
				$item['ting1rate']=price($item['tingnum']*100/$item['shangmenall']);
			}
			if($item['tingnum']>0){
				$item['ting2rate']=price($item['tingnum2']*100/$item['tingnum']);
			}
			if($item['tingnum2']>0){
				$item['xue1rate']=price($item['xuenum']*100/$item['tingnum2']);
			}
			$item['baonum']=pdo_count('jy_yxw_infee_log',$baonumparams);
			$item['hebaonum']=pdo_count('jy_yxw_infee_log',$hebaonumparams)/2;
			$item['myhebaonum']=pdo_count('jy_yxw_infee_log',$myhebaonumparams)/2;
			$item['baonum']=$item['baonum']-$item['hebaonum']+$item['myhebaonum'];
			if($item['xuenum']>0){
				$item['bao1rate']=price($item['baonum']*100/$item['xuenum']);
			}
			if($item['shangmenall']>0){
				$item['bao2rate']=price($item['baonum']*100/$item['shangmenall']);
			}
			$item['danfee1']=pdo_getsum('jy_yxw_infee_log',$baonumparams2,'fee');
			if($item['xuenum']>0){
				$item['bao1rate']=price($item['baonum']*100/$item['xuenum']);
			}
			$item['tuifee']=pdo_getsum('jy_yxw_tuifee_log',$tuifeenumparams,'tuifee');
			$item['tuiisfee']=pdo_getsum('jy_yxw_tuifee_log',$tuifeenumparams,'isfee');
			$item['tuiisfee']=$item['tuiisfee']?:0;
			$item['yestuiisfee']=pdo_getsum('jy_yxw_tuifee_log',$yestuifeenumparams,'isfee');
			//销售业绩
			$item['now']=$this->model('count')->getyeji($item,$between);
			//上月业绩
			$item['yes']=$this->model('count')->getyeji($item,array($yesyue,$nowyue));
			$item['storetitle']=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'title');
			$item['paixu']=$item['now']+$item['shangmen2rate'];
		}
	}
	$heji=array('title'=>'合计');
	paixu($list,'paixu','desc');
	foreach($list as $k=>&$v){
		$v['rank']=$k+1;
		$heji['shangmenall'] +=$v['shangmenall'];
		$heji['shangmenall2'] +=$v['shangmenall2'];
		$heji['tingnum'] +=$v['tingnum'];
		$heji['tingnum2'] +=$v['tingnum2'];
		$heji['xuenum'] +=$v['xuenum'];
		$heji['baonum'] +=$v['baonum'];
		$heji['tuinum'] +=$v['tuinum'];
		$heji['now'] +=$v['now'];
		$heji['yes'] +=$v['yes'];
	}
	if($heji['memberall']>0){
		$heji['shangmen2rate']=price($heji['shangmenall2']*100/$heji['memberall']);
	}
	if($heji['shangmenall']>0){
		$heji['ting1rate']=price($heji['tingnum']*100/$heji['shangmenall']);
	}
	if($heji['tingnum']>0){
		$heji['ting2rate']=price($heji['tingnum2']*100/$heji['tingnum']);
	}
	if($heji['tingnum2']>0){
		$heji['xue1rate']=price($heji['xuenum']*100/$heji['tingnum2']);
	}
	if($heji['xuenum']>0){
		$heji['bao1rate']=price($heji['baonum']*100/$heji['xuenum']);
	}
	if($heji['shangmenall']>0){
		$heji['bao2rate']=price($heji['baonum']*100/$heji['shangmenall']);
	}
	$list[]=$heji;
	$header=array(
		'title'=>'姓名',
		'shangmenall'=>'总接待',
		'tingnum'=>'体验',
		'tingnum2'=>'体用',
		'xuenum'=>'试用',
		'baonum'=>'成交',
		'tuinum'=>'退成交',
		'ting1rate'=>'接待-体验率',
		'ting2rate'=>'体验体用率',
		'xue1rate'=>'体用试用率',
		'bao1rate'=>'试用成交率',
		'bao2rate'=>'总成交率',
		'now'=>'销售业绩',
		'rank'=>'排名',
		'yes'=>'上月业绩',
	);
	model('core/tools')->export2excel($header,$list,'销售员转化率数据');
}